﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class HotelInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Calendar_start.Style.Add("display", "enable");
            Calendar_end.Style.Add("display", "enable");
            TextBox_start.Attributes.Add("onfocus", "document.getElementById('" + Calendar_start.ClientID + "').style.display=''");
            TextBox_end.Attributes.Add("onfocus", "document.getElementById('" + Calendar_end.ClientID + "').style.display=''");
        }

    }

    protected void Calendar_start_SelectionChanged(object sender, EventArgs e)
    {
        TextBox_start.Text = Calendar_start.SelectedDate.ToShortDateString();
    }
    protected void Calendar_end_SelectionChanged(object sender, EventArgs e)
    {
        TextBox_end.Text = Calendar_end.SelectedDate.ToShortDateString();
    }


    protected void Button_Store_Click(object sender, EventArgs e)
    {

        DateTime roomstarttime, roomendtime;
        DateTime.TryParse(TextBox_start.Text, out roomstarttime);
        DateTime.TryParse(TextBox_end.Text, out roomendtime);

        

        if (DateTime.Now.Month!=9&&DateTime.Now.Month!=10&&DateTime.Now.Month!=11)
        {
            Response.Write("<script language=javascript >alert('不在可以录入房间信息规定时间!');</script>");
        }
        else if (TextBox_RoomNum.Text == "")
        {
            Response.Write("<script language=javascript >alert('请输入房间号!');</script> ");
        }else if(TextBox_Price.Text=="")
        {
            Response.Write("<script language=javascript >alert('请输入房间价格!');</script> ");
        }else if (TextBox_start.Text == "")
        {
            Response.Write("<script language=javascript >alert('请输入起始日期!');</script> ");
        }else if (TextBox_end.Text == "")
        {
            Response.Write("<script language=javascript >alert('请输入结束日期!');</script> ");
        }
        else if (DateTime.Compare(roomstarttime,roomendtime)>0)
        {
            Response.Write("<script language=javascript >alert('起始日期大于结束日期!');</script> ");
        }
        else if (!((roomstarttime.Month==12)||(roomstarttime.Month<=3))||!((roomendtime.Month==12)||(roomendtime.Month<=3)))
        {
            Response.Write("<script language=javascript >alert('日期未在规定时间段内!规定时间为：12月到次年3月');</script> ");
        }
        /*else if (RadioButton_State.SelectedValue == "")
        {
            Response.Write("<script language=javascript >alert('请选择房间状态!');</script> ");
        }*/
        else
        {

            string ConnSql = System.Configuration.ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
            //声明Conn为一个SQL Server连接对象
            SqlConnection Conn = new SqlConnection(ConnSql);
            Conn.Open();		//打开连接
            {
                SqlFormat.beginTran(Conn);

                string roomstr = TextBox_RoomNum.Text;

                //字符分割，分割多个房间名称
                string[] arrStr = roomstr.Split(',');

                bool insertOK = true;
                foreach (string roomname in arrStr)
                {
                    Room myroom = new Room((string)Session["hotelid"], roomname, TextBox_Price.Text, RoomStyle.SelectedValue, TextBox_start.Text, TextBox_end.Text, "0");
                    string result = myroom.insert(Conn);
                    if (!"ok".Equals(result))
                    {
                        Response.Write("<script language=javascript >alert('" + result + "');</script> ");
                        insertOK = false;
                        break;
                    }
                        
                }


                if (insertOK)
                {
                    SqlFormat.commit(Conn);
                    Response.Write("<script language=javascript >alert('已存储房间信息!');</script> ");
                    
                }
                else
                {
                    SqlFormat.rollback(Conn);
                    Response.Write("<script language=javascript >alert('存储未成功!');</script> ");
                }
                    

/*

                String a = "Insert into RoomTable values (\'1234567890\',\'" + TextBox_RoomNum.Text + "\',\'" + TextBox_start.Text + "\',\'" + TextBox_end.Text + "\'," + RoomStyle.SelectedValue + "," + TextBox_Price.Text + ",\'" + RadioButton_State.SelectedValue + "\');";
                SqlDataAdapter da = new SqlDataAdapter();		//创建DataAdapter对象
                da.InsertCommand = new SqlCommand(a, Conn);
                if (da.InsertCommand.ExecuteNonQuery() == 1)
                    Response.Write("<script language=javascript >alert('已存储房间信息!');</script> ");
                else
                    Response.Write("<script language=javascript >alert('存储未成功!');</script> ");*/

            }


            Conn.Close();
        }
    }
}